package com.example.home.adapter;

import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.example.home.activity.R;
import com.example.home.viewmodel.classify.CategoryList;

import java.util.List;

public class ClassifyAdapter extends BaseAdapter {

    private Context ctx;
    private List<CategoryList> categoryList;

    public ClassifyAdapter(Context ctx, List<CategoryList> categoryList) {
        this.ctx = ctx;
        this.categoryList = categoryList;
    }

    @Override
    public int getCount() {
        return this.categoryList.size();
    }

    @Override
    public Object getItem(int position) {
        return this.categoryList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return this.categoryList.get(position).getId();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if(convertView == null){
            convertView = LayoutInflater.from(this.ctx).inflate(R.layout.classify_left_item,null);
            viewHolder = new ViewHolder();
            viewHolder.classify_left_line = convertView.findViewById(R.id.classify_left_line);
            viewHolder.Classify_left_text = convertView.findViewById(R.id.classify_left_text);
            convertView.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if(this.categoryList.get(position).getChecked()){
            viewHolder.classify_left_line.setVisibility(View.VISIBLE);
            viewHolder.Classify_left_text.setTextColor(Color.RED);
        }else {
            viewHolder.classify_left_line.setVisibility(View.INVISIBLE);
            viewHolder.Classify_left_text.setTextColor(Color.BLACK);
        }

        viewHolder.Classify_left_text.setText(this.categoryList.get(position).getName());
        return convertView;
    }

    static class ViewHolder {
        TextView classify_left_line;
        TextView Classify_left_text;
    }
}
